package forumSystem;

public class ForumDataBase 
{
	/**

	private String adminUsername;
	private String adminPassword;
	

	private int nextForumID, nextSubForumID, nextUserID, nextPostID, nextReplyID;

	
	private Vector<Forum> myForums;
	private Vector<Integer> myForumsIDs;
	
	private Vector<User> myUsers;
	//private Vector<Integer> myUsersIds;
	
	private Vector<Post> myPosts;
	private Vector<Integer> myPostsIDs;
	
	private Vector<Reply> myReplies;
	
	private Vector<SubForum> mySubForums;
	private Vector<Integer> mySubForumsIDs;
	
	private TreeMap<Integer, String> users_passwords;
	private TreeMap<Integer, Integer> moderators;

	
	public void initialize(String adminUsername, String pass) 
	{
		
		nextForumID = 0;
		nextSubForumID = 0;
		nextUserID = 0;
		nextPostID = 0;

		nextReplyID = 0;

		
		this.adminUsername = adminUsername;
		this.adminPassword = pass;
		
		
		myForums = new Vector<Forum>();
		mySubForums = new Vector<SubForum>();
		myUsers = new Vector<User>();
		myPosts = new Vector<Post>();
		
		myForumsIDs = new Vector<Integer>();
		mySubForumsIDs = new Vector<Integer>();
		myReplies = new Vector<Reply>();
		myPostsIDs = new Vector<Integer>();
		
		users_passwords = new TreeMap<Integer, String>(); 
		moderators = new TreeMap<Integer, Integer>();
		addUser(adminUsername, pass, -1);
		//init Vectors
	}

	
	public String getAdminUserName() 
	{
		return adminUsername;
	}

	
	public String getAdminPass() 
	{
		return adminPassword;
	}

	


	
	public int addForum(String name, String desc) 
	{
		if(name != null)
		{
			for(Forum f : myForums) if(f.getName().equals(name)) return -1; //already exists
			nextForumID++;
			Forum toAdd = new Forum(nextForumID, name, desc);
			myForums.add(toAdd);
			myForumsIDs.add(nextForumID);
			return nextForumID;
		}
		
		return -1;
	}
	
	public int addSubForum(String name, String desc, int parent, int moderatorId) 
	{
		if(name != null && desc != null && myForumsIDs.contains(parent) )
		{	
			nextSubForumID++;
			if(!subForumExists(name, nextSubForumID))
			{
				SubForum toAdd = new SubForum(nextSubForumID, parent, name, desc, moderatorId);
				mySubForums.add(toAdd);
				mySubForumsIDs.add(nextSubForumID);
				return nextSubForumID;
			}
			else
				nextSubForumID--;
		}
			
		return -1;
		
	}


	public int addManagerToForum(int forumID, int userid) 
	{
		if(forumID > 0 && userid >0)
		{
			Iterator<Forum> forum= myForums.iterator();
			Forum curr = forum.next();
			while(forum.hasNext())
			{
				curr = forum.next();
				if(curr.getId() == forumID)
				{
					curr.setAdminID(userid);
					return forumID;
				}
			}
			
		}
		return -1;
	}

	
	public int addForumWithManager(String forumName, String forumDesc, int userID) 
	{
		if(forumName != null && forumDesc != null && userID >= 0)
		{
			
			if(!forumExists(forumName, nextForumID))
			{
				nextForumID++;
				Forum toAdd = new Forum(nextForumID, forumName, forumDesc, userID);
				myForums.add(toAdd);
				myForumsIDs.add(nextForumID);
				return nextForumID;
			}
		}
		return -1;
	}

	
	public int addModerateToSubForum(int subForumID, int moderateID) 
	{
		if(subForumID > 0 && moderateID > 0)
		{
			boolean subForumExists = false;
			Iterator<SubForum> curr = mySubForums.iterator();
			
			while(curr.hasNext() && !subForumExists) {
				if(curr.next().getId() == subForumID)
					subForumExists = true;
			}
			
			if(subForumExists)
			{
				moderators.put(subForumID, moderateID);
				return subForumID;
			}
		}
		return -1;
	}

	
	public Vector<Forum> getForumlist() 
	{
		return myForums;
	}

	
	public Vector<SubForum> getSubForumslist(int parent) 
	{
		if(myForumsIDs.contains(parent))
		{
			Vector<SubForum> result = new Vector<SubForum>();
			Iterator<SubForum> iter = mySubForums.iterator();
			SubForum curr;
			while(iter.hasNext())
			{
				curr = iter.next();
				if(curr.getParentForum() == parent)
					result.add(curr);
			}
			return result;
		}
		else
			return null;
	}

	
	public Forum getForumByID(int forumID) 
	{
		Iterator<Forum> curr = myForums.iterator();
		Forum result;
		while(curr.hasNext())
		{
			result = curr.next();
			if(result.getId() == forumID)
				return result;
		}
		return null;
	}

	
	public SubForum getSubForumByID(int subforumID) 
	{
		Iterator<SubForum> curr = mySubForums.iterator();
		SubForum result;
		while(curr.hasNext())
		{
			result = curr.next();
			if(result.getId() == subforumID)
				return result;
		}
		return null;
	}

	
	public int addUser(String username, String pass, int forumId) 
	{
		if(username != null && pass != null)
		{
			if(!userExist(username))
			{
				nextUserID++;
				User toAdd = new User(nextUserID, Rank.MEMBER, username, pass, forumId);
				myUsers.add(toAdd);
				users_passwords.put(nextUserID, pass);
				return nextUserID;
			}
		}
		return -1; 
	}

	
	public User getUserByID(int id) 
	{
		if(id > 0)
		{
			Iterator<User> users = myUsers.iterator();
			User curr;
			while(users.hasNext())
			{
				curr = users.next();
				if(curr.getId() == id)
					return curr;
			}
		}
		return null;
	}

	
	public int changeUserRank(int userID, Rank rank) 
	{
		if(rank != null)
		{
			User u = getUserByID(userID);
			if(u != null)
			{ 
				u.setRank(rank);
				return userID;
			}
		}
		return -1;
	}

	
	public int addPost(int userID, int subforumID, String title, String desc) 
	{
		if(		users_passwords.containsKey(userID) 
				&& mySubForumsIDs.contains(subforumID)
				&& title != null && desc != null )
		{
			nextPostID++;
			Post toAdd = new Post(nextPostID, title, desc, userID, subforumID);
			myPosts.add(toAdd);
			myPostsIDs.add(nextPostID);
			return nextPostID;
			
		}
		return -1;
	}

	
	public Vector<Post> getSubForumPosts(int subforumID)
	{
		Vector<Post> result = new Vector<Post>();
		Iterator<Post> posts = myPosts.iterator();
		Post curr;
		while(posts.hasNext())
		{
			curr = posts.next();
			if(curr.getSubForumID() == subforumID)
				result.add(curr);
		}
		
		return result;
	}

	
	public int addReply(int userID, int postID, String content) 
	{
		if(users_passwords.containsKey(userID) && postID > 0 && content != null)
		{
			nextReplyID++;
			Reply toAdd = new Reply(nextReplyID, content, userID, postID);
			myReplies.add(toAdd);
			return nextReplyID;
		}
		return -1;
	}

	
	public Vector<Reply> getPostReplies(int postID) 
	{
		if(myPostsIDs.contains(postID))
		{
			Vector<Reply> result = new Vector<Reply>();
			Iterator<Reply> replies = myReplies.iterator();
			Reply curr;
			while(replies.hasNext())
			{
				curr = replies.next();
				if(curr.getPostID() == postID)
				{
					result.add(curr);
				}
			}
			return result;
		}
		else
			return new Vector<Reply>();
	}

	
	private boolean userExist(String username)
	{
		boolean result = false;
		Iterator<User> users = myUsers.iterator();
		while(users.hasNext() && ! result) {
			if(users.next().getUsername().equals(username))
				result = true;
		}
		return result;
	}
	
	private boolean forumExists(String name, int id)
	{
		for(Forum f : myForums) if(f.getName().equals(name)) return true;
		return false;
	}
	
	private boolean subForumExists(String name, int id)
	{
		boolean found = false;
		for (int i = 0; i < mySubForums.size() && !found ; i++) {
			if(mySubForums.elementAt(i).getTitle().equals(name)
				|| mySubForums.elementAt(i).getId() == id)
					found = true;		
		}
		return found;
	}
	
	
	/*
	public static void main(String[] args)
	{
		ForumDataBase checked = new ForumDataBase();
		checked.initialize("evyatar", "password" );
		
		//add forums
		String names = "abcdefghijklmnop";
		for(int i=0;i<names.length(); i++ )
			checked.addForum(names.substring(i), "");
		
		//add subForums partly exist..
		for(int i=0; i<names.length(); i++)
		{
			checked.addSubForum(names.substring(i), "", i*3);
		}
		
		System.out.println( checked.getForumlist() + ", \r\n " +
							checked.getSubForumslist(5) + ",\r\n " +
							checked.getSubForumslist(6) );
		System.out.println( "fix :)");
		
		// addUser addPost - getPsost etc
		String userNames = "evyatarSivan";
		String userPass  = "passworkkkkk"; 
		Vector<Integer> usersAdded = new Vector<Integer>();
		Vector<Integer> subforumswithPosts = new Vector<Integer>();
		for (int i = 0; i < userNames.length(); i++) {
			usersAdded.add(checked.addUser(userNames.substring(i), userPass.substring(i)));
		}
		
		for (int i = 0; i <userNames.length(); i++) {
			subforumswithPosts.add(checked.addPost(usersAdded.elementAt(i), i, "", ""));
		}
		System.out.println( usersAdded + "\r\n" + subforumswithPosts);
		//change rank
		User u = checked.getUserByID(3);
		System.out.println(
		u.getId() +" \r\n"+
		checked.changeUserRank(u.getId(), Rank.SUPERADMIN) + "\r\n" +
		u.getRank());
		
		System.out.println(" also fix :)");
		
		//add manager to forum
		int fId = checked.addForum("evy", "yatar");
		System.out.println("success: " + checked.addManagerToForum(fId, u.getId()));
		//fail test:
		System.out.println("fail: " + checked.addManagerToForum(fId, -1));
		
		//add reply
		SubForum sfid = checked.getSubForumByID(subforumswithPosts.elementAt(0));
		if(sfid != null){
			int postid = checked.addPost(1, sfid.getId(), "", "");
		System.out.println("reply: " + checked.addReply(2, postid , ""));
		System.out.println("ok reply:" + checked.getPostReaplies(postid)); }
		//fail test 
		System.out.println("fail reply: " + checked.addReply(2, -1, "maaaaaaa"));
		
		System.out.println("fixxxx");
				
	}

	
	
	public boolean isUserExist(String username, String pass, int forumID) {
		boolean found = false;
		for (int i = 0; i < myUsers.size() && !found ; i++) {
			if(myUsers.elementAt(i).getUsername().equals(username)
					&& myUsers.elementAt(i).getPass().equals(pass)
					&& myUsers.elementAt(i).getForumIDRegisteredTo()==forumID)
					found = true;
		}
		return found;
	}

	
	public Vector<Post> getForumPosts(int forumID) {
		
		Vector<SubForum> subForums = getSubForumslist(forumID);
		Vector<Post> result = new Vector<Post>();
		for (int i = 0; i < subForums.size(); i++) {
			result.addAll(getSubForumPosts(subForums.elementAt(i).getId()));
		}
		return result;
	}

	
	public User getUserByUserName(String username) {
		for (int i = 0; i < myUsers.size(); i++) {
			if(myUsers.elementAt(i).getUsername().equals(username))
				return myUsers.elementAt(i);
		}
		return null;
	}

	
	public Vector<User> getUsers() {
		return myUsers;
	}

	
	public Post getPostByID(int id) {
		if(id > 0)
		{
			Iterator<Post> posts = myPosts.iterator();
			Post curr;
			while(posts.hasNext())
			{
				curr = posts.next();
				if(curr.getId() == id)
					return curr;
			}
		}
		return null;
	}

	public int getUserRank(int i) 
	{
		User u = this.getUserByID(i);
		if(u == null) return -1;
		return u.getRank().getValue();
	}

	public int getSubForumIdByTopicId(int TopicId) 
	{
		Post t = this.getPostByID(TopicId);
		if(t == null) return -1;
		return t.getSubForumID();
	}

	public int getForumIdByTopicId(int TopicId) 
	{
		Post t = this.getPostByID(TopicId);
		if(t == null) return -1;
		SubForum sf = this.getSubForumByID(this.getSubForumIdByTopicId(TopicId));
		if(sf == null) return -1;
		
		return sf.getParentForum();
	}

	public int isModerator(int userId, int subforumID) 
	{
		SubForum sf = this.getSubForumByID(subforumID);
		if(sf == null) return -1;
		
		return sf.isModeratorId(userId) ? 1 : 0;
	}

	public int removeTopic(int topicId) 
	{
		int ans = 0;
		for(Integer i : myPostsIDs)
		{
			if(i.intValue() == topicId)
			{
				myPostsIDs.remove(i);
				ans = 1;
				break;
			}
		}
		if(ans == 0) return -1;
		for(Post p : myPosts)
		{
			if(p.getId() == topicId)
			{
				myPosts.remove(p);
				break;
			}
		}
		return 1;
	}

	public int getParentForumID(int subforumID) 
	{
		SubForum sf = this.getSubForumByID(subforumID);
		if(sf == null) return -1;
		
		return sf.getParentForum();
	}

	public int removeSubForum(int subForumId) 
	{
		boolean found = false;
		for(Integer i : mySubForumsIDs)
		{
			if(i.intValue() == subForumId)
			{
				found = true;
				mySubForumsIDs.remove(i);
				break;
			}
		}
		if(!found) return -1;
		for(SubForum sf : mySubForums)
		{
			if(sf.getId() == subForumId)
			{
				mySubForums.remove(sf);
				break;
			}
		}
		return 1;
	}

	public int removeModeretor(int user, int subforum) 
	{
		SubForum sf = this.getSubForumByID(subforum);
		if(sf == null) return -1;
		if(sf.removeModeratorId(user)) return 1;
		return 0;
	}

	public Vector<Post> getUserPosts(int userId) 
	{
		User u = this.getUserByID(userId);
		Vector<Post> ans = new Vector<Post>();
		if(u == null) return ans;
		for(Post p : myPosts)
		{
			if(p.getUserID() == userId) ans.add(p);  
		}
		return ans;
	}

	public Vector<String> getCommonUsernames() 
	{
		Vector<String> ans = new Vector<String>();
		Set<Integer> s = new TreeSet<Integer>();
		for(User u : myUsers)
		{
			//int forum = u.getForumIDRegisteredTo();
			if(s.contains(u.getId()) && !ans.contains(u.getUsername())) 
			{
				ans.add(u.getUsername());
			}
			else s.add(u.getId());
		}
		return ans;
		
	}

	public Vector<User> getUsersReplyToUserID(int userId) 
	{
		Vector<User> ans = new Vector<User>();
		for(Reply r : myReplies)
		{
			int replyAuthor = r.getUserID();
			int postId = r.getPostID();
			Post p = this.getPostByID(postId);
			if(p != null && p.getUserID() == userId)
			{
				User u = this.getUserByID(replyAuthor);
				if(u != null) ans.add(u);
			}
		}
		return ans;
	}


	public boolean isUserExist2(String username, String pass) {
		boolean found = false;
		for (int i = 0; i < myUsers.size() && !found ; i++) {
			if(myUsers.elementAt(i).getUsername().equals(username)
					&& myUsers.elementAt(i).getPass().equals(pass))
					found = true;
		}
		return found;
	}


	public int addAdmin(int userID, int forumID) {
		if(userExist(getUserByID(userID).getUsername()))
		{
			getForumByID(forumID).setAdminID(userID);
			return 1;
		}
		return -1;
	}


	public Reply getReplyByID(int replyID) {
		if(replyID > 0)
		{
			Iterator<Reply> replies = myReplies.iterator();
			Reply curr;
			while(replies.hasNext())
			{
				curr = replies.next();
				if(curr.getId() == replyID)
					return curr;
			}
		}
		return null;
	}

	

	**/
}
